from typing import List


class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        res = []
        nums.sort()
        length = len(nums)
        for i in range(length):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            for j in range(i + 1, length):
                if j > i + 1 and nums[j] == nums[j - 1]:
                    continue
                left, right = j + 1, length - 1
                while left < right:
                    if left > j + 1 and nums[left] == nums[left - 1]:
                        left += 1
                        continue
                    while right - 1 > left and nums[i]+nums[j]+nums[left]+nums[right-1] >= target:
                        right -= 1
                    s = nums[i]+nums[j]+nums[left]+nums[right]
                    if s == target:
                        res.append([nums[i], nums[j], nums[left], nums[right]])
                    left += 1
        return res


nums = [-3,-2,-1,0,0,1,2,3]
target = 0
print(Solution().fourSum(nums, target))
